package com.example.composableexample

import android.net.Uri
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.FrameLayout
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.ProgressiveMediaSource
import androidx.media3.ui.AspectRatioFrameLayout
import androidx.media3.ui.PlayerView





/**
 * author: LiLiang
 * createTime: 2022/9/21-11:37
 * remark:
 **/
@Composable
fun VideoPlayer(uri: Uri) {
    val context = LocalContext.current
    val exoPlayer = remember {
        ExoPlayer.Builder(context).build().apply {
            val defaultDataFactory = DefaultDataSource.Factory(context)
            val dataSourceFactory = DefaultDataSource.Factory(context, defaultDataFactory)
            val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
                .createMediaSource(MediaItem.fromUri(uri))
            setMediaSource(mediaSource)
            prepare()
        }
    }

    exoPlayer.playWhenReady = true
    exoPlayer.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
    exoPlayer.repeatMode = Player.REPEAT_MODE_ONE

    DisposableEffect(
        AndroidView(
            factory = {
                PlayerView(context).apply {

                    hideController()
                    useController = false
                    resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
                    player = exoPlayer
                    layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
                }
            }
        )
    ) {
        onDispose { exoPlayer.release() }
    }
}

data class Spotlight(
    val id: Int,
    val video: String,
    val userImage: String,
    val userName: String,
    val isLiked: Boolean = false,
    val likesCount: Int,
    val comment: String,
    val commentsCount: Int
) {
    /**
     * 获取项目内的视频
     *
     * @return
     */
    fun getVideoUrl(): Uri {
//        return Uri.parse("asset:///$video")
        return Uri.parse(video)
    }
}

object DummySpotlightData {
    val spotlight = listOf(
        Spotlight(
            id = 1,
            video = "https://media.w3.org/2010/05/sintel/trailer.mp4",
            userImage = "https://konachan.net/data/preview/91/71/9171c459d9e8a891e03d034a627e2e5d.jpg",
            userName = "Farhan Roy",
            isLiked = true,
            likesCount = 778,
            commentsCount = 156,
            comment = "Wkwkwk..."
        ),
        Spotlight(
            id = 2,
            video = "https://media.w3.org/2010/05/sintel/trailer.mp4",
            userImage = "https://konachan.net/data/preview/b2/ed/b2edcf18d42c0b22fab18060f50aa1ad.jpg",
            userName = "Muhammad Ali",
            isLiked = true,
            likesCount = 5923,
            commentsCount = 11,
            comment = "Awas kamu ya klo pergi"
        ),
        Spotlight(
            id = 3,
            video = "https://media.w3.org/2010/05/sintel/trailer.mp4",
            userImage = "https://konachan.net/data/preview/83/21/8321193db62f8621c017a1ff0f26666e.jpg",
            userName = "Christian Juned",
            isLiked = true,
            likesCount = 2314,
            comment = "Es krim dingin sedapp",
            commentsCount = 200
        ),
        Spotlight(
            id = 4,
            video = "https://media.w3.org/2010/05/sintel/trailer.mp4",
            userImage = "https://konachan.net/data/preview/b2/ed/b2edcf18d42c0b22fab18060f50aa1ad.jpg",
            userName = "Cak Jhon",
            isLiked = true,
            likesCount = 786,
            comment = "Ados slurr",
            commentsCount = 700
        ),
        Spotlight(
            id = 5,
            video = "https://media.w3.org/2010/05/sintel/trailer.mp4",
            userImage = "https://konachan.net/data/preview/82/70/8270fb21541511111fde50806aa538e3.jpg",
            userName = "David Dulkader",
            isLiked = true,
            likesCount = 1890,
            comment = "Kerjaan di tengah hutan",
            commentsCount = 232
        )

    )
}